//msp430-gcc -mmcu=msp430g2553 -o main.elf main.c

//#include <msp430.h>
#include <msp430g2553.h>
#include <string.h>

#include "lcd/msp.h"
#include "lcd/lcd.h"
//#include "lcd/graphics.h"
#include "lcd/color.h"
//#include "lcd/typedefs.h"
//#include "lcd/tests.h"

#define LED1 BIT0
#define LED2 BIT6

//port2
#define RST BIT1
#define CS BIT0
#define RS BIT2

//port1
#define SDA BIT7
#define SCL BIT6

//input
#define ADCIn BIT3

#define p1on(x) {P1OUT |= x;}
#define p1off(x) {P1OUT &= ~x;}
#define p2on(x) {P2OUT |= x;}
#define p2off(x) {P2OUT &= ~x;}

void writeData(unsigned char data);
void writeCommand(unsigned char data);

volatile unsigned int i;
void delayd(unsigned int);
unsigned char TXData;
unsigned char TXByteCtr;

unsigned char orientation = 0;

int main(void)
{
	WDTCTL = WDTPW + WDTHOLD;		//stop watchdog timer
	
	BCSCTL1 = CALBC1_1MHZ;
	DCOCTL = CALDCO_1MHZ;
	BCSCTL2 = DIVS_2 + DIVM_0;
		
	UCB0CTL1 = UCSWRST;			//enable sw reset
	
	P1DIR |= LED1;
	P1OUT |= LED1;
	P2DIR |= CS + RS + RST;			
	P2OUT |= CS + RS + RST;
	P1SEL |= SDA + SCL;			//assign i2c
	P1SEL2 |= SDA + SCL;

	UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC;	//i2c master, synch mode
	UCB0CTL1 = UCSSEL_2 + UCSWRST;		//use smclk, keep sw rst
	UCB0BR0 = 12;				//fSCL=SMCLK/12 = 100kHz
	UCB0BR1 = 0;

	UCB0I2CSA = 0X48;			//set slave address
	UCB0CTL1 &= ~UCSWRST;			//clear sw rst, resume op
	UCB0I2CIE |= UCSTTIE;			//en stt interrupt
	IE2 |= UCB0TXIE;			//enable tx interrupt
	
	TXData = 0x00;				//used to hold data
	
	p2off(CS + RST);
	p2on(CS + RST);
	
	initLCD();

	delayd(75);
	
	for(;;)
	{
		//write_command(0xff);
		p1off(LED1);
		delayd(65535);
		p1on(LED1);
		delayd(65535);

		_delay_cycles(1600000);

		clearScreen(1);

		setColor(COLOR_16_RED);
		drawString(5, 5, FONT_SM, "Texas Instruments");
		setColor(COLOR_16_WHITE);
		drawString(5, 20, FONT_MD, "2.2\" 320x240 BoosterPack");
		setColor(COLOR_16_BLUE);
		drawString(5, 40, FONT_MD, "& MSP430F5529 LaunchPad");
		setColor(COLOR_16_ORANGE);
		drawString(5, 60, FONT_MD, "RobG's graphics library");
		setColor(COLOR_16_PURPLE);
		drawString(5, 80, FONT_MD, "Works with:");
		setColor(COLOR_16_YELLOW);
		drawString(5, 100, FONT_LG, "F5172 F5510 F5529");
		setColor(COLOR_16_GREEN_YELLOW);
		drawString(5, 120, FONT_LG, "G2553 G2955 & more");

		_delay_cycles(32000000);

		drawTILogo(56, 56, COLOR_16_RED);
		drawTILogo(55, 56, COLOR_16_RED);
		drawTILogo(56, 55, COLOR_16_RED);
		drawTILogo(55, 55, COLOR_16_RED);
		drawTILogo(50, 50, COLOR_16_WHITE);

		_delay_cycles(40000000);
		shesGotColors(100);

		_delay_cycles(12000000);
//
		clearScreen(1);
//
//		drawLogicLines(8);
//
//		_delay_cycles(16000000);

		//drawSpirograph(40, 20, 15);
		//_delay_cycles(16000000);

		setOrientation(++orientation & 0x03);
	}
}

void delayd(unsigned int d)
{
	volatile unsigned int i;
	for(i = 0; i < d; i++)
	{
		nop();
	}
}

#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(USCIAB0TX_VECTOR))) USCIAB0TX_ISR (void)
#else
#error Compiler not supported!
#endif
{
  if (TXByteCtr)                            // Check TX byte counter
  {
    UCB0TXBUF = TXData;                     // Load TX buffer
    TXByteCtr--;                            // Decrement TX byte counter
  }
  else
  {
    UCB0CTL1 |= UCTXSTP;                    // I2C stop condition
    IFG2 &= ~UCB0TXIFG;                     // Clear USCI_B0 TX int flag
    __bic_SR_register_on_exit(CPUOFF);      // Exit LPM0
  }
}


void Reset()
{
	p2off(RST);
	delayd(100);
	p2on(RST);
	delayd(100);
}

void writeCommand(unsigned char data)
{
	p2on(CS);
	p2off(RS);
	TXByteCtr = 1;			//load tx byte counter
	while(UCB0CTL1 & UCTXSTP);	//ensure stop sent
	UCB0CTL1 |= UCTR + UCTXSTT;	//enable lpm0 w/int
	__bis_SR_register(CPUOFF + GIE);
	data++;
	p2off(CS);
}

void writeData(unsigned char data)
{
	p2on(CS);
	p2on(RS);
	TXByteCtr = 1;			//load tx byte counter
	while(UCB0CTL1 & UCTXSTP);	//ensure stop sent
	UCB0CTL1 |= UCTR + UCTXSTT;	//enable lpm0 w/int
	__bis_SR_register(CPUOFF + GIE);
	data++;
	p2off(CS);
}

